package chatclient.tools;

import chatclient.applets.Chat;
import java.util.Vector;

public class Idler extends Thread {

    public Idler(Chat chat, Vector vector) {
        listener = chat;
        times = vector;
        lastActivity = System.currentTimeMillis();
        lastWarning = 0L;
        done = false;
        start();
    }

    public void activity() {
        lastActivity = System.currentTimeMillis();
        synchronized (this) {
            notify();
        }
    }

    @Override
    public void run() {
        synchronized (this) {
            while (!done) {
                long l = System.currentTimeMillis();
                long l1 = 0L;
                int i;
                for (i = 0; l1 <= l && i < times.size(); i++) {
                    l1 = lastActivity + 1000L * (long) ((Integer) times.elementAt(i)).intValue();
                }

                try {
                    wait(l1 > l ? l1 - l : 1L);
                } catch (InterruptedException interruptedexception) {
                }
                l = System.currentTimeMillis();
                if (l >= l1) {
                    int j = (int) ((l - lastActivity) / 1000L);
                    listener.idle(i - 1, j, ((Integer) times.elementAt(times.size() - 1)).intValue() - j);
                }
                lastWarning = l;
            }
        }
    }

    public void quit() {
        done = true;
        synchronized (this) {
            notify();
        }
    }
    private Chat listener;
    private Vector times;
    private long lastActivity;
    private long lastWarning;
    private boolean done;
}
